package com.buer.storage.controller;

import com.buer.storage.commom.JsonResult;
import com.buer.storage.dto.CarDto;
import com.buer.storage.dto.OutCarprodToAreaDto;
import com.buer.storage.dto.OutStockToCarDto;
import com.buer.storage.dto.SelectCarNumDto;
import com.buer.storage.entity.Car;
import com.buer.storage.entity.Carprod;
import com.buer.storage.service.CarService;
import com.buer.storage.service.CarprodstockdetailsService;
import com.buer.storage.vo.R;
import com.buer.storage.vo.StockDetailVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/car")
public class CarController {
    @Resource
    private CarService carService;

    @Autowired
    private CarprodstockdetailsService carprodstockdetailsService;

    //增加车辆信息
    @PostMapping("/insertCarCategorytwo")
    public R add(@RequestBody Car car) {
        if (carService.insertCar(car) > 0) {
            return R.setR(1, "添加车辆信息成功", null);
        }
        return R.setR(0, "添加车辆信息失败", null);
    }

    //删除车辆信息
    @GetMapping("/deleteCarCategoryById")
    public R delete(Integer carId) {
        if (carService.deleteCarById(carId) > 0) {
            return R.setR(1, "删除车辆信息成功", null);
        }
        return R.setR(0, "删除车辆信息失败", null);
    }

    //修改车辆信息
    @PostMapping("/updateCarCategoryById")
    public R update(@RequestBody Car car){
        if (carService.updateCarById(car)>0){
            return R.setR(1,"修改车辆信息成功",null);
        }
        return R.setR(0,"修改车辆信息失败",null);
    }

    //查询所有车辆信息
    @GetMapping("/selectCarAll")
    public List<CarDto> selectCarAll(){
        return carService.selectCarAll();
    }

    @PostMapping("/getCar")
    public JsonResult getCar(@RequestBody Map<String,Object> params) {
        PageInfo<Car> entityList = carService.getCar(params);
        Map result=new HashMap<>();
        result.put("total",entityList.getTotal());
        result.put("list",entityList.getList());
        return new JsonResult(1,result);
    }

    //提交商品详情进行累加
    @PostMapping("/submitAddCarprod")
    public R submitAddCarprod(@RequestBody List<OutStockToCarDto> outStockToCarDto) {
       try {
           return carService.submitAddCarprod(outStockToCarDto);
       }catch (Exception e){
           e.printStackTrace();
           return R.setR(0, "车载装备添加失败", null);
       }
    }

    //提交商品详情进行累加
    @PostMapping("/unloadDeleteCarprod")
    public R unloadDeleteCarprod(@RequestBody List<OutCarprodToAreaDto> outCarprodToAreaDto) {
        try {
            return carService.unloadDeleteCarprod(outCarprodToAreaDto);
        }catch (Exception e){
            e.printStackTrace();
            return R.setR(0, "车载装备卸载失败", null);
        }
    }


    @PostMapping("/getCarprodDetailsByCarprodId")
    public R getCarprodDetailsByCarprodId(@RequestBody Map<String,Object> params) {
        List<StockDetailVo> list=carprodstockdetailsService.getCarprodDetailsByCarprodId(params);
        return R.ok(list);

    }


    @GetMapping("/selectCarByName")
    public List<CarDto> selectCarByName(String carNumber){
       return carService.selectCarByName(carNumber);
    }

    @GetMapping("/selectCarprodByCarid")
    public List<Carprod> selectCarprodByCarid(Integer carId){
        return carService.selectCarprodByCarid(carId);
    }

    @GetMapping("/selectCarNum")
    public List<SelectCarNumDto > selectCarNum(){
        return carService.selectCarNum();
    }
}
